package lambda.jdkfunctioninterface.转换型;

import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import org.testng.annotations.Test;

/**
 * Bi是Bidirectional【双向】的简写
 * 接受两个参数，返回一个结果
 * @author
 */
public class BiFunction1 {

    /**
     * 四则运算
     */
    @Test
    public void t1() {
        BiFunction1 biFunction1 = new BiFunction1();

        //        System.out.println(biFunction1.compute(4, 2, (value1, value2) -> value1 + value2));
        System.out.println(biFunction1.compute(4, 2, Integer::sum));
        System.out.println(biFunction1.compute(4, 2, (v1, v2) -> v1 - v2));
        System.out.println(biFunction1.compute(4, 2, (v1, v2) -> v1 * v2));
        System.out.println(biFunction1.compute(3, 2, (v1, v2) -> v1 / v2));


        // 求和后平方
        System.out.println(biFunction1.calculate(3, 4, Integer::sum, v -> v * v));

    }

    public int compute(int num1, int num2, BiFunction<Integer, Integer, Integer> biFunction) {
        return biFunction.apply(num1, num2);
    }

    public int calculate(int num1, int num2, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function){
        //  调用addThen首先对接收的两个参数进行biFunction的apply，然后再进行function的apply
        return biFunction.andThen(function).apply(num1, num2);
    }

    @Test
    private void t2() {
        BiFunction<Integer, Integer, Integer> bi1 = (BinaryOperator<Integer>)(o1, o2) -> o1 - o2;
        BiFunction<Integer, Integer, Integer> bi2 = (o1, o2) -> o1 - o2;
        var bi3 = (BinaryOperator<Integer>)(o1, o2) -> o1 - o2;

        Integer apply = bi2.apply(1, 6);
        System.out.println(apply);


    }



}
